package com.jci.vsd.activity;

import android.Manifest;
import android.app.IntentService;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.text.TextUtils;
import android.widget.Toast;

import com.jci.vsd.R;
import com.jci.vsd.application.VsdApplication;
import com.jci.vsd.constant.AppConstant;
import com.jci.vsd.downloadUtils.BGADownloadProgressEvent;
import com.jci.vsd.downloadUtils.BGAUpgradeUtil;
import com.jci.vsd.utils.AutoInstall;
import com.jci.vsd.utils.Loger;
import com.jci.vsd.view.widget.SimpleToast;

import java.io.File;

import rx.Subscriber;
import rx.functions.Action1;

/**
 * Created by liqing on 18/8/16.
 * 下载
 */

public class DownloadIntentService extends IntentService {
    private String mApkUrl = "";
    private String mNewVersion = "1.0.8";
//    mApkUrl = "https://cs.royalsecurity.cn/shuidao/notoken/downapk";

    private NotificationCompat.Builder builder;
    private NotificationManager notificationManager;

    public DownloadIntentService() {
        super("DownloadIntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        mApkUrl = AppConstant.BASE_URL+"shuidao/notoken/downapk";
        if (intent != null) {

            notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= 26) {
                Loger.e("---SDK_INT>=26");
                String id = "channel_1";
                String description = "123";
                int importance = NotificationManager.IMPORTANCE_DEFAULT;

                NotificationChannel mChannel = new NotificationChannel(id, "123", importance);
                mChannel.setDescription(description);

                mChannel.enableLights(true);//是否在桌面icon右上角展示小红点
                mChannel.setLightColor(Color.RED);
//                mChannel.enableVibration(true);
//                mChannel.setShowBadge(true);
//                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                notificationManager.createNotificationChannel(mChannel);
                String channelId = mChannel.getId();
                Loger.e("---channelId=" + channelId);
                builder = new NotificationCompat.Builder(getApplicationContext(), channelId);


            } else {
                builder = new NotificationCompat.Builder(getApplicationContext(), null);
            }


            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("正在获取资源中。。。");
            builder.setAutoCancel(true);
            builder.setContentText("版本更新");

              notificationManager.notify(0, builder.build());

            downloadApkFile();
        }
    }

    public void goToNotificationSettings() {
        Intent i = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
        i.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
        startActivity(i);
    }
    public void downloadApkFile() {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};


        // 下载新版 apk 文件

        BGAUpgradeUtil.downloadApkFile(mApkUrl, mNewVersion)
                .subscribe(new Subscriber<File>() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onCompleted() {
//                            dismissDownloadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
//                            dismissDownloadingDialog();
                    }

                    @Override
                    public void onNext(File apkFile) {
                        Loger.e("--onNext--" + apkFile.getPath());

//                        AutoInstall.setUrl(apkFile.getPath());
//
//                        AutoInstall.install(getApplicationContext());
                        AutoInstall.installApk(apkFile);

//                        BGAUpgradeUtil.installApk(apkFile);

                    }
                });


        BGAUpgradeUtil.getDownloadProgressEventObservable()
//                .compose(this.<BGADownloadProgressEvent>bindToLifecycle())
//                .compose(this.<BGADownloadProgressEvent>)

                .subscribe(new Action1<BGADownloadProgressEvent>() {
                    @Override
                    public void call(BGADownloadProgressEvent downloadProgressEvent) {


                        int progress = (int) Math.round(downloadProgressEvent.getProgress() / (double) downloadProgressEvent.getTotal() * 100);
                        builder.setContentInfo(String.valueOf(progress) + "%").setProgress(100, progress, false);
                        builder.setContentTitle("下载中。。。");
                        notificationManager.notify(0, builder.build());
                        Loger.e("--progress" + progress);

//                        if (!TextUtils.isEmpty(mApkUrl)) {
//                            mDownloadingDialog.setProgress(downloadProgressEvent.getProgress(),
//                                    downloadProgressEvent.getTotal());
//                        }
                    }

                });
    }
}


